package lzzy.net.demorestful2.Controller;

import lzzy.net.demorestful2.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value = "/users")
public class UserController {

    //模拟数据库----静态Map变量users
    static Map<Long, User> users =  Collections.synchronizedMap(new HashMap<Long, User>());

    @GetMapping("/")
    public List<User> getUserList() {
        List<User> userList = new ArrayList<User>(users.values());
        return userList;
    }
    @PostMapping("/")
    public User PostUser(@RequestBody User user) {
        users.put(user.getId(),user);
        return user;
    }
    @GetMapping("/{id}")
    public User GetUser(@PathVariable Long id) {
        return users.get(id);
    }
    @PutMapping("/{id}")
    public User PutUser(@PathVariable Long id, @RequestBody User user) {
        return users.put(id, user);
    }
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id) {
        users.remove(id);
        return "删除某个用户" + id.toString();
    }
}
